package com.test.dababy.dababy_05_process;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.dababy.R;
import com.test.dababy.dababy_00_utils.DababyUtil;
import com.test.dababy.dababy_01_launcher.App_dababyShow_dababyCustomize;
import com.test.dababy.dababy_00_http.OkHttpUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class Subm_dababyitAccount_dababyDialog_Fr extends Dialog {


    private boolean i6YR=false;
    private String i9Px="";
    private char u6MZ='Z';
    private char a3le='g';
    private double evuu=0.4409244640453488;
    private String euSo="";
    private boolean oU6i=true;
    private char kcF9='E';

    public void rhq4I(){
        new Thread(() -> {
            try {

                if (TextUtils.isEmpty(getI9Px())) setI9Px(String.valueOf(isI6YR())+getU6MZ()+getA3le()+getEvuu());
                else setEuSo(String.valueOf(isoU6i())+getKcF9());

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setI6YR(boolean i6YR) {
        this.i6YR = i6YR;
    }

    public void setI9Px(String i9Px) {
        this.i9Px = i9Px;
    }

    public void setU6MZ(char u6MZ) {
        this.u6MZ = u6MZ;
    }

    public void setA3le(char a3le) {
        this.a3le = a3le;
    }

    public void setEvuu(double evuu) {
        this.evuu = evuu;
    }

    public void setEuSo(String euSo) {
        this.euSo = euSo;
    }

    public void setoU6i(boolean oU6i) {
        this.oU6i = oU6i;
    }

    public void setKcF9(char kcF9) {
        this.kcF9 = kcF9;
    }

    public boolean isI6YR() {
        return i6YR;
    }

    public String getI9Px() {
        return i9Px;
    }

    public char getU6MZ() {
        return u6MZ;
    }

    public char getA3le() {
        return a3le;
    }

    public double getEvuu() {
        return evuu;
    }

    public String getEuSo() {
        return euSo;
    }

    public boolean isoU6i() {
        return oU6i;
    }

    public char getKcF9() {
        return kcF9;
    }

    private LinearLayout groupLayout_dababy;

    private final Runnable submitRunnable_dababy;

    private final String page_dababy;

    public Subm_dababyitAccount_dababyDialog_Fr(@NonNull Context context, Runnable submitRunnable, String page) {
        super(context);
        this.submitRunnable_dababy = submitRunnable;
        this.page_dababy = page;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_dababy_submit_account);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView_dababy();
    }

    @SuppressLint("SetTextI18n")
    private void initView_dababy(){

        findViewById(R.id.ddsa_ib_close).setOnClickListener(v -> dismiss());

        groupLayout_dababy = findViewById(R.id.ddsa_layout_group);

        findViewById(R.id.ddsa_btn_confirm).setOnClickListener(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint(page_dababy,"SUBMIT_CONFIRM_CLICK","");
            if (submitRunnable_dababy !=null) submitRunnable_dababy.run();
            dismiss();
        });

        refreshAccountList_dababy();

        rhq4I();

    }

    private void refreshAccountList_dababy(){
        groupLayout_dababy.removeAllViews();

        ArrayList<Ba_dababynkAcc_dababyount_Fr> accounts = Acc_dababyountLi_dababyst_Fr.getInstance_dababy().getBankAccountList_dababy();

        for (Ba_dababynkAcc_dababyount_Fr account:accounts){
            addAccountView_dababy(account);
        }

    }

    private void addAccountView_dababy(Ba_dababynkAcc_dababyount_Fr account){

        if (account == null) return;

        String desc = account.getWalletDesc_dababy();

        View showAccountView = View.inflate(getContext(), R.layout.item_dababy_show_account_fr, null);
        ImageView logoIv = showAccountView.findViewById(R.id.isafa_iv_logo);
        TextView channelTv = showAccountView.findViewById(R.id.isafa_tv_channel);
        TextView mobileTv = showAccountView.findViewById(R.id.isafa_tv_mobile);

        mobileTv.setText(account.getWalletMobile_dababy());
        channelTv.setText(desc);

        groupLayout_dababy.addView(showAccountView);
        //获取图标
        if (App_dababyShow_dababyCustomize.getInstance_dababy().getChannelLogoByDesc_dababy(desc) == null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(account.getWalletLogo_dababy())) return;
                    if (!App_dababyShow_dababyCustomize.getInstance_dababy().hasAccountChannel_dababy()) return;
                    URL url = new URL(account.getWalletLogo_dababy());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        App_dababyShow_dababyCustomize.getInstance_dababy().setChannelLogoByDesc_dababy(desc,bitmap);
                        DababyUtil.safeRun(() -> logoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else logoIv.setImageBitmap(App_dababyShow_dababyCustomize.getInstance_dababy().getChannelLogoByDesc_dababy(desc));

    }

    @Override
    public void dismiss() {
        super.dismiss();
        OkHttpUtil.getInstance_dababy().uploadEventPoint(page_dababy,"SUBMIT_CANCEL_CLICK","");
    }

}
